package com.example.itas;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.example.classes.IP;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class JoinActivity extends Activity {

	ProgressDialog dialog;
	EditText join_name, join_id, join_pw, join_conf_pw;
	Button join_commit;

	private final String TAG = "JoinActivity";
	
	IP ip = new IP();
	private final String HOST = ip.ip;
	private final int PORT = 8080;
	private final String PATH = "/IServer-war/webresources/entities.userlist/join";
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join);
        join_name = (EditText) findViewById(R.id.join_et_name);
        join_id = (EditText) findViewById(R.id.join_et_id);
        join_pw = (EditText) findViewById(R.id.join_et_pw);
        join_conf_pw = (EditText) findViewById(R.id.join_et_confpw);
        join_commit = (Button) findViewById(R.id.join_btn_ok);

        join_commit.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				AsyncJoin aj = new AsyncJoin();
				aj.execute();
			}
		});
    }
    
    private class AsyncJoin extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {

			dialog = ProgressDialog.show(JoinActivity.this, "",
					"Now Joinning", true);
		}

		@Override
		protected String doInBackground(String... params) {

			String result = "false";

			// create() method of the web service needs a Person object as input
			String id = join_id.getText().toString();
			String pwd = join_pw.getText().toString(); 
			String conf_pwd = join_conf_pw.getText().toString();
			String name = join_name.getText().toString();
			
			HttpHost target = new HttpHost(HOST, PORT);
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(PATH);

			// set the MIME type for POST message body.
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");

			HttpEntity entity = null;

			List<NameValuePair> idpair = new ArrayList<NameValuePair>();
			idpair.add(new BasicNameValuePair("ID", id));
			idpair.add(new BasicNameValuePair("PWD", pwd));
			idpair.add(new BasicNameValuePair("NAME", name));

			try {
				post.setEntity(new UrlEncodedFormEntity(idpair));

				HttpResponse response = client.execute(target, post);

				// Status code 204 is success with "No content".
				// This happens when a RESTful method returns void.
				if (response.getStatusLine().getStatusCode() < 400) {
					result = "get";
				}

				entity = response.getEntity();

				String str = EntityUtils.toString(entity);

				// convert JSON to Person
				result = str;

				Log.d(TAG, "" + response.getStatusLine().getStatusCode());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.i("errorr:", "UnsupportedEncodingException");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				Log.i("errorr:", "ClientProtocolException");
			} catch (IOException e) {
				e.printStackTrace();
				Log.i("errorr:", "IOException");
			}
			return result;
		}
		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();
			
			if (result.equals("YES")) {
				Toast.makeText(JoinActivity.this, "Join success!", Toast.LENGTH_SHORT)
						.show();
				finish();
			} else if (result.equals("NO")) {
				Toast.makeText(JoinActivity.this, "Check your ID or password",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(JoinActivity.this, "Error!", Toast.LENGTH_SHORT).show();
			}

		}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_join, menu);
        return true;
    }
}
